/*
Copyright (c) 2009 Sean Mallory

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

#include <iostream>
#include <string>
#include "sptlib.h"

#define IOBUFFSIZE 8*1024*1024

void usage(char* argzero) {
	std::cerr << argzero << 
	" <head SAS source file> [sasautos=<sasautodir>[,<autodir>,...]]\n"
	"\tsasautos: comma separated, no spaces, list of SASAUTO directories.. Default = \"\"\n"
	<< std::flush;
}

int sasgrep(std::string sourcefile, std::string searchfor) { \
// read sourcefile, output lines matching searchfor
	int ret = 0;
	//char* *obuffer, *ibuffer;
	char *ibuffer;
	std::string temp, tmp;
	
	//std::ofstream fulfile;
	//obuffer = new char[IOBUFFSIZE];
	//fulfile.rdbuf()->pubsetbuf(obuffer,IOBUFFSIZE);
	//fulfile.open(searchfor.c_str(), std::ios::out);
	//fulfile.close();
	
	std::ifstream sasfile;
	ibuffer = new char[IOBUFFSIZE];
	sasfile.rdbuf()->pubsetbuf(ibuffer,IOBUFFSIZE);
	sasfile.open(sourcefile.c_str(), std::ifstream::in);
	
	bool	result =std::getline(sasfile, temp);
	if (!result) std::cerr << "Problem reading " << sourcefile << std::endl;
	while (result) {
		ret++;
		std::string::size_type matchat = temp.find(searchfor, 0);
		if (matchat !=  std::string::npos)
		    std::cout << sourcefile << " (" << ret << "):" << temp << std::endl;
		result =getline(sasfile, temp);
	}
	sasfile.close();
	return ret;
}
int main(int argc,char *argv[]) {
	// D'oh?
	if (argc < 3) {usage(argv[0]); return(1);};
	int ret = 0;
	// Parse parse
		parmMap parms = smParms(argc, argv, true);
		int parmposition = 0;
		std::string sasfile = "";
		if (parms.count("file") > 0) sasfile= parms["file"];
		if (parms.count("sasfile") > 0) sasfile= parms["sasfile"];
		if (parms.count("sas") > 0) sasfile= parms["sas"];
		if (parms.count("sasfile") > 0) sasfile= parms["sasfile"];
		if (sasfile == "") {
			std::stringstream tmp;
			tmp << "-" << parmposition;
			std::string argname = tmp.str();
			 if (parms.count(argname) > 0) {
				sasfile= parms[argname];
				parmposition ++;
			 } else {
				 std::cerr << "No input file." << std::endl;
				 return(1);
			 };
		}

		std::string findstr = "";
		if (parms.count("find") > 0) findstr= parms["find"];
		if (parms.count("f") > 0) findstr= parms["f"];
		if (findstr  == "") {
			std::stringstream tmp;
			tmp << "-" << parmposition;
			std::string argname = tmp.str();
			 if (parms.count(argname) > 0) {
				findstr = parms[argname];
				parmposition ++;
			 } else {
				 std::cerr << "No output file." << std::endl;
				 return(1);
			 };
		}

	//for(parmMap::iterator iter = parms.begin(); iter != parms.end(); iter++)  {
	//      cout << (*iter).first << " = " << iter->second << endl;}
		
	// open what needs to be opened
	// run the process
	int read = sasgrep(sasfile, findstr) ;
	std::cerr << read << " lines read." << std::endl;
	return ret;
}
